<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  userInfo: {
    type: Object,
    required: true
  },
  isEditing: {
    type: Boolean,
    required: true
  }
})

const emit = defineEmits(['update:userInfo', 'update:isEditing'])

// 头像上传相关
const avatarUploadUrl = '/api/user/avatar'
const avatarUploadHeaders = {
  Authorization: `Bearer ${localStorage.getItem('token')}`
}

// 头像上传成功
const handleAvatarSuccess = (response) => {
  const newUserInfo = { ...props.userInfo, avatar: response.data.url }
  emit('update:userInfo', newUserInfo)
  ElMessage.success('头像上传成功')
}

// 头像上传之前的处理
const beforeAvatarUpload = (file) => {
  const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
  const isLt2M = file.size / 1024 / 1024 < 2

  if (!isJPG) {
    ElMessage.error('头像只能是 JPG 或 PNG 格式!')
  }
  if (!isLt2M) {
    ElMessage.error('头像大小不能超过 2MB!')
  }
  return isJPG && isLt2M
}

// 保存用户信息
const saveUserInfo = () => {
  // 这里应该调用API保存用户信息
  // API调用后端
  // updateUserInfoAPI(props.userInfo).then(() => {
  //   ElMessage.success('保存成功')
  //   emit('update:isEditing', false)
  // })
  
  // 模拟保存成功
  setTimeout(() => {
    ElMessage.success('保存成功')
    emit('update:isEditing', false)
  }, 500)
}
</script>

<template>
  <el-card class="info-card">
    <div class="user-header">
      <el-upload
        class="avatar-uploader"
        :action="avatarUploadUrl"
        :headers="avatarUploadHeaders"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
        :disabled="!isEditing"
      >
        <el-avatar :size="100" :src="userInfo.avatar" />
        <el-icon v-if="isEditing" class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
      <div class="user-basic-info">
        <h2>{{ userInfo.nickname }}</h2>
        <p>{{ userInfo.username }}</p>
      </div>
      <div class="header-actions">
        <el-button v-if="!isEditing" type="primary" @click="emit('update:isEditing', true)">编辑资料</el-button>
        <template v-else>
          <el-button @click="emit('update:isEditing', false)">取消</el-button>
          <el-button type="primary" @click="saveUserInfo">保存</el-button>
        </template>
      </div>
    </div>

    <el-form :model="userInfo" label-width="100px" :disabled="!isEditing">
      <el-form-item label="昵称">
        <el-input v-model="userInfo.nickname" />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="userInfo.email" />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="userInfo.phone" />
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="userInfo.gender">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生日">
        <el-date-picker v-model="userInfo.birthday" type="date" placeholder="选择日期" />
      </el-form-item>
    </el-form>
  </el-card>
</template>

<style scoped>
.info-card {
  margin-bottom: 20px;
}

.user-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.user-basic-info {
  margin-left: 20px;
  flex-grow: 1;
}

.user-basic-info h2 {
  margin: 0;
  font-size: 24px;
}

.user-basic-info p {
  margin: 5px 0 0;
  color: #666;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.avatar-uploader {
  position: relative;
  cursor: pointer;
}

.avatar-uploader:hover .avatar-uploader-icon {
  opacity: 1;
}

.avatar-uploader-icon {
  position: absolute;
  bottom: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 4px;
  border-radius: 50%;
  opacity: 0;
  transition: opacity 0.3s;
}
</style>